var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderstate:0,
    pageSize:6,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    chatList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
    itemData:[],
    currentTab:0,
    tabs: ['奖惩', '排查',],
    tabs_state: [1,2],
    record_type:1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	this.data.pageIndex = 1;
	this.setData({
		chatList:[],
		isLoading:true,
	})
    this.onHttpInit()
  },
  onChange:function(event) {
    console.log('onChange',event)
    var index = event.detail.index
    var record_type = this.data.tabs_state[index]
    this.data.record_type = record_type;
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      chatList:[]
    });
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/estate/chat/list', {
      state:0,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex,
    }, function (res) {
      console.log("对话列表数据", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1
        if (res.data.totalcount > 0) {
          var chats = res.data.data.chats;
          console.log('chats',chats)
          for (var index in chats) {
            that.data.chatList.splice(that.data.chatList.length, 0, chats[index]);
          }
          that.setData({
            hasData: true,
            chatList: that.data.chatList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goDetail(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pagesProperty/pages/chat/detail/index?id='+ id,
    })
  },
  goPub(){
    var isLogin = app.getStorageSync("isLogin");
    if(isLogin == 0){
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      wx.navigateTo({
        url: '/pagesProperty/pages/chat/add/index',
      })
    }
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index].imgUrl)
    }
    common_Js.preImg(a, c)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshchatList()
  },
  refreshchatList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      chatList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onHttpInit()
  },
})